home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- COrderedList.c
-
- COrderedList maintains an ordered cluster of objects. It also adds methods
- for iterating over the ordered list.
-
- Copyright (C) 1992 by Brown University. All rights reserved.
-
- Permission is granted to any individual or institution to use, copy,
- or redistribute the binary version of this software and its
- documentation provided this notice and the copyright notices are
- retained. Permission is granted to any individual or non-profit
- institution to use, copy, modify, or redistribute the source files
- of this software provided this notice and the copyright notices are
- retained. This software may not be distributed for profit, either
- in original form or in derivative works, nor can the source be
- distributed to other than an individual or a non-profit institution.
- Any individual or group interested in seeing and/or using these
- source files but who are prevented from doing so by the above
- constraints should contact Don Wolfe, Vice-President for Computer
- Systems at Brown University, (401) 863-7247, for possible
- software licensing of the source developed at Brown.
-
- Brown University and Andrew James Gilmartin make no representations
- about the suitability of this software for any purpose.
-
- BROWN UNIVERSITY AND ANDREW JAMES GILMARTIN GIVE NO WARRANTY, EITHER
- EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
- INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
- WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
-
- AUTHOR: Andrew_Gilmartin@Brown.Edu
- MODIFIED: 93-02-04
-
- ******************************************************************************/
-
- #include "COrderedList.h"
-
-
- /******************************************************************************
- IOrderedList
-
- Initialize the list. Note that if no compare function is specified then
- use the defualt.
- ******************************************************************************/
-
- void COrderedList::IOrderedList( CompareFunc compare )
- {
- IUnorderedList( compare );
-
- } /* IOrderedList */
-
-
-
- /******************************************************************************
- FindIndex
-
- Find the offset of the object in the items list. This method uses bisection
- to narrow down where the object is in the list. If the object is found then
- return TRUE, otherwise return FALSE.
- ******************************************************************************/
-
- Boolean COrderedList::FindIndex( CObject* anObject, long* foundindex )
- {
- long left;
- long right;
- long index;
- long relation;
-
- left = 0;
- right = numItems - 1;
-
- while( left <= right )
- {
- index = ( left + right ) / 2;
-
- relation = (*fCompare)( anObject, (CObject*) (*items)[ index ] );
-
- if ( relation < 0 )
- right = index - 1;
- else if ( relation > 0 )
- left = index + 1;
- else
- {
- *foundindex = index + 1;
- return TRUE;
- }
- }
-
- *foundindex = left + 1;
- return FALSE;
-
- } /* FindIndex */
-
-
-
- /******************************************************************************
- Add
-
- Add the object to the list in order. Note that this method does not check
- whether the object already exists in the list.
- ******************************************************************************/
-
- void COrderedList::Add( CObject* anObject )
- {
- long index;
-
- FindIndex( anObject, &index );
- InsertAtIndex( &anObject, index );
-
- } /* Add */
-
-
-
- /******************************************************************************
- Reorder
-
- This method re-sorts the content of the list.
- ******************************************************************************/
-
- void COrderedList::Reorder( void )
- {
- COrderedList* list;
-
- list = (COrderedList*) this->Copy();
-
- this->RemoveAll();
- this->AddAll( list );
-
- list->Dispose();
-
- // /* An untested alternative.... */
- //
- // HLock( hItems );
- // qsort( *hItems, numItems, elementSize, fCompare );
- // HUnlock( hItems );
-
- } /* Reorder */
-